<template>
	<view style="position: fixed;width: 100%; top: 0;">
		<view class="danmu-li" v-for="(item,index) in listData" :class="item.type" :style="[item.style]" :key="index">
			<view class="danmu-inner">
				<view class="user-box">
					<view class="" @click="btnclick(item.id)" style="display: flex;justify-content: space-between;">
						<view class="user-img">
							<view class="img-box">
								<image :src="item.avatar == null ? 'https://img95.699pic.com/xsj/10/tm/0h.jpg%21/fh/300' :  item.avatar">
								</image>
							</view>
						</view>
						<view class="user-text cl1">
							{{item.content}}
						</view>
					</view>
					<view class="" @click="trigger(item.id,index)">
						<image :src="getImgUrl('/img/missimg/whitexin.png')"
							style="width: 20px;height: 25px;margin-left: 10px;" v-if="item.isLike == 1"></image>
						<image :src="getImgUrl('/img/missimg/orangexin.png')"
							style="width: 20px;height: 25px;margin-left: 10px;" v-else-if="item.isLike == 0"></image>
					</view>
					<view class="" style="color: #FFF0B3;font-weight: 400;">
						{{item.likeNumber}}
					</view>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		queryByld,
		Riverlike
	} from '../../utils/api.js'
		import { imgBaseUrl,baseURL } from '../../utils/baseUrl.js'
	export default {
		props: {
			//rightToLeft leftToRight leftBottom
			type: {
				type: String,
				default: 'rightToLeft'
			},
			minTime: {
				type: Number,
				default: 8
			},
			maxTime: {
				type: Number,
				default: 8
			},
			minTop: {
				type: Number,
				default: 300
			},
			maxTop: {
				type: Number,
				default: 600
			},
			hrackH: { //轨道高度
				type: Number,
				default: 50,
			},
			noStacked: { //不允许堆叠(暂不可用)
				type: Array,
				default () {
					return []
				}
			},
			newlistData: {
				type: Array,
				default () {
					return []
				}
			}
		},
		data() {
			return {
				listData: [],
			}
		},
		mounted() {
			this.hrackNum = Math.floor((this.maxTop - this.minTop) / this.hrackH);
		},
		methods: {
			newadd() {
				if (this.type == 'rightToLeft') {
						for (let i = 0; i < this.newlistData.length; i++) {
							setTimeout(() => {
								this.add(this.newlistData[i])
								// console.log(this,'this............');
								if(i  == this.newlistData.length - 1 && this.newlistData.length > 0)
								{
									this.$parent.Data()
									setTimeout(()=>{
										
										this.$parent.colrdo()
									},1000)
									
								}
							}, i * 2000)
						}
				}
			},
			add(obj) {
				// console.log(obj, 'obj')
				let data = {
					content: obj.content,
					avatar: obj.avatar,
					id: obj.id,
					likeNumber: obj.likeNumber,
					isLike: obj.isLike,
					// name: obj.content,
					// id: Date.parse(new Date()),
					time: Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime)),
					type: this.type
				}
				if (this.type === 'leftBottom') {
					let objData = {
						content: data.content,
						avatar: data.avatar,
						id: data.id,
						likeNumber: data.likeNumber,
						isLike: data.isLike,
						// name: data.content,
						type: 'leftBottomEnter',
						style: {
							transition: `all 1.5s`,
							animationDuration: `1.5s`,
							transform: `translateX(0%)`,
							bottom: `${this.minTop}px`
						}
					}
					let listLen = this.listData.length;
					let hrackNum = this.hrackNum;
					for (let i in this.listData) {
						if (this.listData[i].status === 'reuse') { //重用
							this.$set(this.listData, i, objData);
						} else if (this.listData[i].status === 'reset') { //重置
							this.listData[i].style.transition = 'none';
							this.listData[i].style.bottom = 0;
							this.listData[i].status = 'reuse';
						} else if (this.listData[i].status === 'recycle') { //回收
							this.listData[i].type = 'leftBottomExit';
							this.listData[i].status = 'reset';
						} else {
							this.listData[i].style.bottom = parseInt(this.listData[i].style.bottom) + this.hrackH + 'px';
						}
						if (parseInt(this.listData[i].style.bottom) >= (this.maxTop - this.hrackH) && this.listData[i]
							.status !== 'reset') { //需要回收
							this.listData[i].status = 'recycle';
						}
					}
					if (listLen < hrackNum + 2) {
						this.listData.push(objData);
					}
				} else if (this.type === 'rightToLeft' || this.type === 'leftToRight') {
					let objData = this.horStacked(data);
					for (let i in this.listData) {
						if (this.listData[i].delTime <= Date.parse(new Date())) {
							this.repaint(i, objData.type);
							objData.type = '';
							this.$set(this.listData, i, objData);
							return
						}
					}
					this.listData.push(objData);
				}
			},
			horStacked(data) {
				return {
					content: data.content,
					avatar: data.avatar,
					id: data.id,
					likeNumber: data.likeNumber,
					isLike: data.isLike,
					// name: data.content,
					type: this.type,
					style: {
						animationDuration: `${data.time}s`,
						top: `${Math.ceil(Math.random() * (this.maxTop - this.minTop + 5) + this.minTop)}px`
					},
					delTime: Date.parse(new Date()) + data.time * 1200
				}
			},
			repaint(index, type) {
				setTimeout(() => {
					this.listData[index].type = type;
				}, 100)
			},
			async btnclick(id) {
				this.$emit('getclick')
				const result = await queryByld(id)

			},
			async trigger(id, index) {
				// 判断点赞变亮0为点赞变亮，1为取消点赞
				if (index, this.listData[index].isLike == 1) {
					this.listData[index].isLike = 0
					this.listData[index].likeNumber = 1

				} else if (this.listData[index].isLike == 0) {
					this.listData[index].isLike = 1
					this.listData[index].likeNumber = 0
				}
				const result = await Riverlike(id)
			},
			// 图片渲染
			getImgUrl(image){
				return imgBaseUrl+image;
			}
		},

	}
</script>
<style>

</style>
<style lang="scss">
	@keyframes leftBottomEnter {
		0% {
			transform: translateY(100%);
			opacity: 0;
		}

		100% {
			transform: translateY(0%);
			opacity: 1;
		}
	}

	@keyframes leftBottomExit {
		0% {
			transform: translateY(0%);
			opacity: 1;
		}

		100% {
			transform: translateY(-200%);
			opacity: 0;
		}
	}

	@keyframes leftToRight {
		0% {
			transform: translateX(-300%);
		}

		100% {
			transform: translateX(100%);
		}
	}

	@keyframes rightToLeft {
		0% {
			transform: translateX(100%);
		}

		100% {
			transform: translateX(-300%);
		}
	}

	.danmu-li {
		position: absolute;
		width: 100%;
		transform: translateX(100%);
		animation-timing-function: linear;
		z-index: 999999;

		&.leftBottomEnter {
			animation-name: leftBottomEnter;
		}

		&.leftBottomExit {
			animation-name: leftBottomExit;
			animation-fill-mode: forwards;
		}

		&.rightToLeft {
			animation-name: rightToLeft;
		}

		&.leftToRight {
			animation-name: leftToRight;
		}

		.danmu-inner {
			display: inline-block;

			.user-box {
				display: flex;
				padding: 3rpx 40rpx 3rpx 10rpx;
				background: rgba(255, 255, 255, 0.3);
				border-radius: 32rpx;
				align-items: center;

				.user-img {
					.img-box {
						display: flex;

						image {
							width: 58rpx;
							height: 58rpx;
							background: rgba(55, 55, 55, 1);
							border-radius: 50%;
						}
					}
				}

				.user-status {
					margin-left: 10rpx;
					white-space: nowrap;
					font-size: 28rpx;
					font-weight: 400;
					color: rgba(255, 255, 255, 1);
				}

				.user-text {
					margin-left: 10rpx;
					white-space: nowrap;
					line-height: 50rpx;
					font-size: 28rpx;
					font-weight: 400;
					min-width: 80rpx;
					color: #FFFFFF;
					font-family: PingFang SC-Regular, PingFang SC;
				}
			}
		}
	}
</style>